<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="mini" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
    <el-form-item label="字典项类别" prop="typeCode">
      <el-input clearable v-model="dataForm.typeCode" placeholder="字典项类别"></el-input>
    </el-form-item>
    <el-form-item label="字典项编码" prop="constantCode">
      <el-input clearable v-model="dataForm.constantCode" placeholder="字典项编码"></el-input>
    </el-form-item>
    <el-form-item label="字典项名称" prop="constantName">
      <el-input clearable v-model="dataForm.constantName" placeholder="字典项名称"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="orderNum">
      <el-input clearable v-model="dataForm.orderNum" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input clearable v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()" :loading="btnLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        btnLoading: false,
        dataForm: {
          id: 0,
          typeCode: '',
          constantCode: '',
          constantName: '',
          orderNum: '',
          remark: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          updateTime: '',
          isDelete: ''
        },
        dataRule: {
          typeCode: [
            { required: true, message: '字典项类别不能为空', trigger: 'blur' }
          ],
          constantCode: [
            { required: true, message: '字典项编码不能为空', trigger: 'blur' }
          ],
          constantName: [
            { required: true, message: '字典项名称不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.requestUrl(`/sys/constantcode/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.requestParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.typeCode = data.info.typeCode
                this.dataForm.constantCode = data.info.constantCode
                this.dataForm.constantName = data.info.constantName
                this.dataForm.orderNum = data.info.orderNum
                this.dataForm.remark = data.info.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            this.$http({
              url: this.$http.requestUrl(`/sys/constantcode/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.requestData({
                'id': this.dataForm.id || undefined,
                'typeCode': this.dataForm.typeCode,
                'constantCode': this.dataForm.constantCode,
                'constantName': this.dataForm.constantName,
                'orderNum': this.dataForm.orderNum,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.btnLoading = false
            })
          }
        })
      }
    }
  }
</script>
